<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span :class="{active:optionsIndex==index}" v-for="(item,index) in options1" :key="index" @click="optionsFn(index,item.min,item.max)">{{item.txt}}</span>
        </li>
        <li>
          <strong>分类：</strong>
          <span  :class="{active:options2Index==index}" v-for="(item,index) in options2" :key="index" @click="options2Fn(index,item.type)">{{item.txt}} </span>
        </li>
      </ul>
      <List :arr="goodListShow" />
      <div v-show="!isReachBottom">
        <i id="loader"></i>loading
      </div>
      <div v-show="isReachBottom">加载完毕</div>
      <!-- <div id="loader"></div>
        <span> {{isReachBottom?'加载完毕':'loding..'}}</span> -->
    </div>
  </div>
</template>                            
<script>

import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import {GoodsSearchAPI} from '@/request/api'
import {getScrollTop,getClientHeight,getScrollHeight} from "@/utils"
export default {
  data() {
    return {
      goodList:[],
      // 栏目
      did:0,
      // 实体产品 
      type:0,
      // 最小积分
      min:0,
      // 最大积分
      max:10000,
      // 关键字
      keyword:'',
      isLoading:false,
      // 排序的数组项
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 分类数组项
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      optionsIndex:0,
      options2Index:0,
     goodListShow: [],
    //  当前页面
     page:1,
    //  每条条数
     size:8,
     isReachBottom:false,
    };
  },
  components: {
    Crumb,
    List,
  },
   created () {
     let keyword =this.$route.query.keyword;
     keyword ||='';
     this.keyword=keyword;  
    this.goodsSearch();
  },
  // 挂载完成后
  mounted () {
    window.addEventListener('scroll',this.scrollFn);
  },
  // 取消监听
  beforeDestroy(){
    window.removeEventListener('scroll',this.scrollFn);
  },
  
  methods: {
    async goodsSearch(){
      let res = await GoodsSearchAPI({
      did:this.did,
      type:this.type,
      min:this.min,
      max:this.max,
      keyword:this.keyword
    });
    console.log(res);
    this.goodList=res.data
     this.goodListShow=this.goodList.filter((item,index)=>index<this.size)
    //  优化bug
    this.page=1;
    this.isReachBottom=false;
    // 判断数据是否加载完毕/是否到达底部
        if(this.goodListShow.length==this.goodList.length){
          // 加载完毕了
          this.isReachBottom=true;
          return;
        }
    },
    optionsFn(idx,min,max){
      // 高亮
      this.optionsIndex=idx;
      this.min=min;
      this.max=max;
      this.goodsSearch();
    },
    options2Fn(idx,type){
      console.log(idx,type);
      this.options2Index=idx;
      this.type=type;
      this.goodsSearch();
    },
    // 滚动函数
    scrollFn(){
      console.log('滚动了',
      getScrollTop(),
      getClientHeight(),
      getScrollHeight()
      );
      if(getScrollTop()+getClientHeight()>getScrollHeight()-240){

       if(this.goodListShow.length==this.goodList.length){
          // 加载完毕了
          this.isReachBottom=true;
          return;
        }


        if(this.isLoading==false){
          this.isLoading=true;
           console.log('准备下一页');
        this.page++;
        setTimeout(()=>{
          console.log('请求中....');
          for(
            let i=(this.page-1)*this.size;
            i<this.page*this.size;
            i++
            ){
          console.log('for:i',this.page,i,this.goodList[i]);
          this.goodList[i]&&this.goodListShow.push(this.goodList[i]);
        }
        this.isLoading=false;
        },2000)
        } 
      }
    }
  },
  watch: {
    '$route.query.keyword':{
      handler(newVal,oldVal){
        this.keyword=newVal;
        this.goodsSearch();
      }
    }
  }
  
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right:20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
#loader {
        border: 5px solid #f3f3f3;
        border-top: 5px solid #555;
        border-radius: 50%;
        width: 10px;
        height: 10px;
        display: inline-block;
        animation: spin 2s linear infinite;
       margin-left: 593px
      }
 
      @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
      }
</style>